import grails.test.*

class UserControllerTests extends ControllerUnitTestCase {

    User user
    UserController uc
    
    protected void setUp() {
        user = new User(userName: "User1", firstName: "User1FN", lastName: "User1LN")
        user.save()

        // Setup the UserController
        uc = new UserController()
    }

    protected void tearDown() {
        user.delete()
    }

    void testHandleLogin() {
        // Setup controller parameters
        uc.params.userName = user.userName
        
        // Call the action
        uc.handleLogin()

        // If action functioned correctly, it put a user object into the session.
        def sessionUser = uc.session.user
        assert sessionUser
        assertEquals("Expected ids to match", user.id, sessionUser.id)

        // Add the user was redirected to the Company Page.
        assertEquals "/Doctor", uc.response.redirectedUrl
    }

//    void testHandlerLoginInvalidUser() {
//        // Setup controller parameters
//        def user = new User(userName: "InvalidUser", firstName: "User1FN", lastName: "User1LN")
////        uc.params.userName = "INVALID_USER_NAME"
//        uc.params.userName = user.userName
//
//        // Call the action
//        uc.handleLogin()
//        assertEquals "/user/login", uc.response.redirectedUrl
//        def message = uc.flash.message
//        assert message
//        assert message.startsWith("User not found")
//    }

    void testLogout() {
        // Make it look like user is logged in
        uc.session.user = user

        uc.logout()
        def sessionUser = uc.session.user
        assertNull("Expected session user to be null", sessionUser)
        assertEquals "/user/login", uc.response.redirectedUrl
    }
}
